﻿namespace ContactsSync.Providers.Facebook
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using ContactsSync.Infrastructure.Models;
    using facebook.Components;
    using Microsoft.Practices.Composite.Events;

    public class FacebookContactsProvider : IContactsProvider
    {
        public event EventHandler<DataEventArgs<IList<Contact>>> RetrieveContactsCompleted;
        
        public string ProviderName
        {
            get { return "Facebook"; }
        }

       public void RetrieveContactsAsync()
       {
           FacebookService facebookService = new FacebookService
           {
               ApplicationKey = "1196c8a01b4f82e67bcef6a1169796fb",
               Secret = "41bac64b254055b9ba605b6de7df4135"
           };

            try
            {
                facebookService.ConnectToFacebook();
                BackgroundWorker backgroundWorker = new BackgroundWorker();
                backgroundWorker.RunWorkerCompleted += this.BackgroundWorker_RunWorkerCompleted;
                backgroundWorker.DoWork += BackgroundWorker_DoWork;
                backgroundWorker.RunWorkerAsync(facebookService);
            }
           catch
           {
               this.OnRetrieveContactsCompleted(new List<Contact>());
           }
       }

       private static void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
       {
           List<Contact> contacts = new List<Contact>();

           FacebookService facebookService = e.Argument as FacebookService;

           if (facebookService != null)
           {
               IList<facebook.Schema.user> friends = facebookService.friends.getUserObjects();

               foreach (facebook.Schema.user friend in friends)
               {
                   Contact contact = new Contact
                                         {
                                             Id = friend.uid.ToString(),
                                             FirstName = friend.first_name,
                                             LastName = friend.last_name,
                                             ImageSource = friend.pic_square,
                                         };

                   contacts.Add(contact);
               }

               contacts.Sort((c1, c2) => string.Compare(c1.FirstName, c2.FirstName, StringComparison.Ordinal));
           }

           e.Result = contacts;
       }

        private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
       {
           IList<Contact> contacts = e.Result as IList<Contact>;

           if (contacts == null)
           {
               contacts = new List<Contact>();
           }

           this.OnRetrieveContactsCompleted(contacts);
       }

       private void OnRetrieveContactsCompleted(IList<Contact> contacts)
       {
           EventHandler<DataEventArgs<IList<Contact>>> completed = this.RetrieveContactsCompleted;
           if (completed != null)
           {
               completed(this, new DataEventArgs<IList<Contact>>(contacts));
           }
       }
    }
}